package zhou.chs.p4_3;

import android.Manifest;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity{
    TextView tvSta;         //显示WiFi状态
    ListView lvW,lvS;       //分别显示WiFi扫描结果和已配置WiFi
    WifiManager mManager;   //WiFi适配器
    WifiInfo info;          //WiFi信息
    WifiBroadCastReceiver mBroadcastReceiver;  //WiFi广播接收
    ArrayList wlist = new ArrayList();  //扫描结果列表
    ArrayList slist = new ArrayList(); //已配置WiFi列表
    ArrayAdapter wadapter,sadapter;  //适配器
    private static final int PERMISSION_REQUEST_COARSE_LOCATION = 1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tvSta=(TextView) findViewById(R.id.idtv);  //获得控件
        lvW=(ListView) findViewById(R.id.idLv1);
        lvS=(ListView) findViewById(R.id.idLv2);
        mManager= (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
        wadapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, wlist);
        lvW.setAdapter(wadapter);  //显示WiFi扫描结果列表
        sadapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, slist);
        lvS.setAdapter(sadapter);  //显示已配置WiFi列表
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {     // Android6.0及以上版本动态位置权限申请
            if (this.checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, PERMISSION_REQUEST_COARSE_LOCATION);
            }
        }
    }
    @Override   //注册WiFi状态变化广播
    protected void onResume(){
        super.onResume();
        mBroadcastReceiver = new WifiBroadCastReceiver();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);
        intentFilter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);
        intentFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
        registerReceiver(mBroadcastReceiver, intentFilter);
    }
    @Override   ////注消广播
    protected void onPause() {
        super.onPause();
        unregisterReceiver(mBroadcastReceiver);
    }
    public void open(View v) {  //打开WiFi适配器
        mManager.setWifiEnabled(true);
    }
    public void close(View v) {  //关闭WiFi适配器
        mManager.setWifiEnabled(false);
    }
    // 自定义WifiBroadCastReceiver类
    class WifiBroadCastReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            switch (intent.getAction()) {
                case WifiManager.WIFI_STATE_CHANGED_ACTION:
                    int wifiState = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE,
                            WifiManager.WIFI_STATE_DISABLED);
                    switch (wifiState) {
                        case WifiManager.WIFI_STATE_DISABLED:
                            tvSta.setText("WiFi已关闭");
                            break;
                        case WifiManager.WIFI_STATE_ENABLED:
                            tvSta.setText("WiFi已打开");
                            break;
                    }
                    break;
                case WifiManager.SCAN_RESULTS_AVAILABLE_ACTION:
                    wlist.clear();
                    slist.clear();
                    List<ScanResult> Results = mManager.getScanResults();   //需要定位权限
                    for (ScanResult Result : Results) {   //扫描结果加入列表
                        wlist.add("SSID:" + Result.SSID + "\nRSSI:" +Result.level);
                    }
                    wadapter.notifyDataSetChanged();  //刷新列表显示
                    List<WifiConfiguration> configurations = mManager.getConfiguredNetworks();
                    for (WifiConfiguration Result : configurations) {
                        slist.add(Result.SSID);     //已配置WiFi加入列表
                    }
                    sadapter.notifyDataSetChanged();  //刷新列表显示
                    break;
                case WifiManager.NETWORK_STATE_CHANGED_ACTION:
                    WifiInfo info = mManager.getConnectionInfo();
                    int ipadr = info.getIpAddress();  //获得已连接WiFi信息
                    String ipstr="";
                    if (ipadr != 0) {
                        ipstr = ((ipadr & 0xff) + "." + (ipadr >> 8 & 0xff) + "."
                                + (ipadr >> 16 & 0xff) + "." + (ipadr >> 24 & 0xff));
                    }
                    tvSta.setText(info.getSSID() + " " + ipstr);  //显示已连接WiFi名称和IP地址
                    break;
            }
        }
    }
}
